/**
 * Created by mac on 2017/5/4.
 */

import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class SDec {
    public static void main(String args[]) throws Exception {
        // 获取密文
        FileInputStream f = getFileInputStream("SEnc.dat");
        int num = getAvailable(f);
        byte[] ctext = getBytes(num);
        f.read(ctext);
        // 获取密钥
        FileInputStream f2 = getFileInputStream("keykb1.dat");
        int num2 = getAvailable(f2);
        byte[] keykb = getBytes(num2);
        f2.read(keykb);
        SecretKeySpec k = new SecretKeySpec(keykb, "DESede");
        // 解密
        Cipher cp = Cipher.getInstance("DESede");
        cp.init(Cipher.DECRYPT_MODE, k);
        byte[] ptext = getBytes(ctext, cp);
        // 显示明文
        String p = getUtf8(ptext);
        System.out.println(p);
    }

    private static byte[] getBytes(byte[] ctext, Cipher cp) throws IllegalBlockSizeException, BadPaddingException {
        return cp.doFinal(ctext);
    }

    private static String getUtf8(byte[] ptext) throws UnsupportedEncodingException {
        return new String(ptext, "UTF8");
    }

    private static byte[] getBytes(int num) {
        return new byte[num];
    }

    private static int getAvailable(FileInputStream f) throws IOException {
        return f.available();
    }

    private static FileInputStream getFileInputStream(String name) throws FileNotFoundException {
        return new FileInputStream(name);
    }
}
